import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let isLogin = uni.getStorageSync('isLogin') || false
let userInfo = uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')) : {}
let token = uni.getStorageSync('token') || ''

const store = new Vuex.Store({
  state: {
    isLogin: isLogin, // 判断是否登录
    userInfo: userInfo,
    token: token
  },
  mutations: {
    isLogin(state, userInfo) {
      console.log(userInfo);
      state.isLogin = true
      state.userInfo = userInfo
      state.token = userInfo.token
      uni.setStorageSync("isLogin", true)
      uni.setStorageSync("userInfo", JSON.stringify(userInfo))
      uni.setStorageSync("token", userInfo.token)
    },
    outLogin(state) {
      state.isLogin = false
      state.userInfo = []
      state.token = ""
    },
    // 修改手机号邮箱或密码
    edituser(state, { key, value }) {
      state.userInfo[key] = value
      uni.setStorageSync("userInfo", JSON.stringify(state.userInfo))
    },
    edituserInfo(state, obj) {
      state.userInfo.username = obj.name

      if (state.userInfo.userinfo) {
        state.userInfo.userinfo.sex = obj.sex
        state.userInfo.userinfo.qg = obj.qg
        state.userInfo.userinfo.job = obj.job
        state.userInfo.userinfo.birthday = obj.birthday
        state.userInfo.userinfo.path = obj.path
      }

      uni.setStorageSync("userInfo", JSON.stringify(state.userInfo))

      // let obj = {
      //   name: this.phone,
      //   sex: this.sex2,
      //   qg: this.qg,
      //   job: this.join,
      //   birthday: this.birthday,
      //   path: this.home,
      // };
    }
  }
})
export default store